package com.pda.xinma.ui.inventory;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.pda.xinma.R;
import com.pda.xinma.adapter.FullInventoryListAdapter;
import com.pda.xinma.bean.FullInventoryListBean;
import com.pda.xinma.contract.FullInventoryListContract;
import com.pda.xinma.presenter.FullInventoryListPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = AppActivityKey.FULLINVENTORYLISTACTIVITY)
public class FullInventoryListActivity extends BaseMvpActivity<FullInventoryListPresenter> implements FullInventoryListContract.View, OnRefreshListener, OnLoadMoreListener {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.rv_content)
    RecyclerView rvContent;
    FullInventoryListAdapter fullInventoryListAdapter;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlLayout;
    private int pager=1;
    @Override
    public FullInventoryListPresenter getPresenter() {
        return new FullInventoryListPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_list_refrash;
    }

    @Override
    public void initView() {
        srlLayout.setOnRefreshListener(this);
        srlLayout.setOnLoadMoreListener(this);
        srlLayout.setEnableRefresh(true);
        tvTitle.setText("待盘库");
        fullInventoryListAdapter = new FullInventoryListAdapter(R.layout.item_scattered_inventory_list, new ArrayList<>());
        rvContent.setAdapter(fullInventoryListAdapter);

    }

    @Override
    public void initLinsenterner() {
        fullInventoryListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("TaskNo", fullInventoryListAdapter.getData().get(position).getTaskNo());
                startIntent(AppActivityKey.FULLINVENTORYTABACTIVITY, bundle);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        srlLayout.autoRefresh();
    }

    @Override
    public void getData() {

    }


    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finishActivity();
    }

    @Override
    public void showMessage(String msg) {
        super.showMessage(msg);
        srlLayout.finishRefresh();
        srlLayout.finishLoadMore();
    }

    @Override
    public void setList(List<FullInventoryListBean> list) {
        srlLayout.finishRefresh();
        srlLayout.finishLoadMore();
        if (pager == 1){
            fullInventoryListAdapter.setNewInstance(list);
        }else {
            fullInventoryListAdapter.addData(list);
        }

    }

    @OnClick(R.id.tv_search)
    public void onViewClicked1() {
        srlLayout.autoRefresh();
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        pager=1;
        presenter.wholePalletList(etSearch.getText().toString(),pager);
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        pager++;
        presenter.wholePalletList(etSearch.getText().toString(),pager);
    }
}
